{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4bdde0ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import argparse\n",
    "import multiprocessing\n",
    "import os\n",
    "import pickle\n",
    "import sys\n",
    "import warnings\n",
    "import xml.etree.ElementTree as ET\n",
    "from copy import deepcopy\n",
    "from multiprocessing import Manager, Pipe\n",
    "from operator import truth\n",
    "from threading import Thread\n",
    "from tqdm import tqdm\n",
    "import cv2\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import scipy.io as sio\n",
    "import spectral.io.envi as envi\n",
    "import torch\n",
    "import torch.distributed as dist\n",
    "import torch.multiprocessing as mp\n",
    "import torch.nn as nn\n",
    "from matplotlib.backends.backend_pdf import PdfPages\n",
    "from numpy import flip\n",
    "from spectral import imshow, view_cube\n",
    "from sshkeyboard import listen_keyboard\n",
    "\n",
    "from AutoGPU import autoGPU\n",
    "from GAN_training_utils import DataResult, TrainProcess, setup_seed\n",
    "# from AutoGPU import autoGPU\n",
    "from models import (_1DCNN, _2DCNN, _3DCNN, _3DCNN_1DCNN, _3DCNN_AM, PURE2DCNN,\n",
    "                    PURE3DCNN, PURE3DCNN_2AM, SAE, SAE_AM, DBDA_network,\n",
    "                    HamidaEtAl, LeeEtAl, SSRN_network, _2dCNN, myknn, mysvm)\n",
    "from myTrans2 import Generator\n",
    "from NViT import ViT as NViT\n",
    "from utils import DataPreProcess, myplot, plot, setpath, splitdata\n",
    "from utils import (DataPreProcess, DataResult, get_imggnd, listen_plot, myplot,\n",
    "                   plot, setpath, splitdata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "33951617",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已选择第8张卡，型号为TITAN RTX，22536MB/24220MB显存可用\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "        try:\n",
    "            autoGPU(1, 5000)\n",
    "            break\n",
    "        except Exception as e :\n",
    "            print(e)\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1b804237",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bd222a5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 18/18 [00:48<00:00,  2.68s/it]\n"
     ]
    }
   ],
   "source": [
    "# gen_model = Generator().to('cuda')\n",
    "import pandas as pd\n",
    "df = pd.DataFrame()\n",
    "\n",
    "\n",
    "for i in tqdm(range(200, 401, 200)):\n",
    "    gen_model = Generator().to('cuda')\n",
    "        # dis_model.load_state_dict(torch.load(resultpath + '1bestmodel.pth'))\n",
    " \n",
    "    gen_model =  torch.load('pathology/032370b/roi2/Split/proportion/Tr_0.05/Va_0.01/Te_0.94/1/result/TransGan.bak/genmodel_epoch{}.pth'.format(i))\n",
    "#     gen_model.load_state_dict({k.replace('module.',''):v for k,v in t.items()}, strict=True)\n",
    "    gen_model.to('cuda')\n",
    "\n",
    "    sample = np.zeros((0, 60, 9, 9))\n",
    "    for _ in range(100): \n",
    "    # processeddata['train'].patch.to('cuda')\n",
    "        noise = torch.FloatTensor(1, 50).to('cuda')\n",
    "        noise.data.resize_(1, 50).normal_(0, 1)\n",
    "\n",
    "        \n",
    "        class_onehot = torch.ones((1, 10)).to('cuda')\n",
    "\n",
    "        noise[np.arange(1), :10] = (class_onehot * 1).unsqueeze(1)\n",
    "        out = gen_model(noise)\n",
    "        out = out.detach().cpu().numpy()\n",
    "        sample = np.vstack((sample, out))\n",
    "        spectral = sample[:,:,4,4].sum(axis=0)\n",
    "        spectral = spectral/100\n",
    "#         df.loc[:,str(i)]=spectral/spectral.max()\n",
    " spectral.to_excel('阳性生成样本.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "bfb4d272",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9.4191158 , 18.48115452, 11.11970872,  3.03243272, 12.00991346,\n",
       "       39.97770971, 37.17358404, 25.40080076, 16.44419989, 46.33271486,\n",
       "       52.67277384, 60.75768846, 46.60933095, 86.36399782, 60.26279926,\n",
       "       41.53438041, 61.01015764, 74.99506474, 55.8094427 , 66.45408022,\n",
       "       84.69588953, 69.89649564, 92.24471498, 69.1323756 , 65.15780383,\n",
       "       72.01386321, 86.86717272, 57.92998499, 85.34505123, 82.49785352,\n",
       "       45.41280651, 19.29312989, 65.46704978, 75.70808226, 85.93934649,\n",
       "       72.98665231, 79.60939515, 91.1305756 , 64.36164898, 67.82483387,\n",
       "       93.54010087, 84.52366036, 27.40003046, 58.19453311, 91.337533  ,\n",
       "       42.50980145, 60.40862614, 25.51170996,  5.05411429,  7.16863699,\n",
       "       19.44113906,  7.88941554,  6.09400947, 20.49240276, 34.93923801,\n",
       "       18.19654143,  0.26384742, 11.67281196,  7.57624631,  2.85176382])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " spectral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3966d506",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练比例为 训练集2000 验证集1000 测试集1000\n",
      "模型为danfengViT\n",
      "Lucky Dog! Patch data already exists!\n",
      "patch 的尺寸为\n",
      "(4000, 60, 9, 9)\n"
     ]
    }
   ],
   "source": [
    "dataset = './pathology/data/032370b-20x-roi2'\n",
    "NTr = 2000\n",
    "trialnumber = 1\n",
    "NTe = 1000\n",
    "NVa = 1000\n",
    "patchsize = 9\n",
    "modelname = 'danfengViT' \n",
    "gpu_num = 1\n",
    "depth = 5\n",
    "load_bestmodel = 1\n",
    "gpu_ids = -1\n",
    "\n",
    "resultpath, imagepath, datapath = setpath(dataset, trialnumber , NTr, \n",
    "                                                NVa, NTe, modelname)\n",
    "    \n",
    "\n",
    "\n",
    "IMAGE, GND = get_imggnd(dataset)\n",
    "print('训练比例为 训练集{} 验证集{} 测试集{}'.format(NTr, NVa, NTe))\n",
    "print('模型为{}'.format(modelname))\n",
    "\n",
    "processeddata = DataPreProcess(IMAGE, patchsize, datapath, 1).processeddata\n",
    "print('patch 的尺寸为') \n",
    "print(processeddata['train'].patch.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d91f4f30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "45.39346941376692\n",
      "45.240757692042315\n",
      "57.48265281669761\n",
      "75.61457409277423\n",
      "100.73950182571558\n",
      "135.59686975599007\n",
      "185.38915689890817\n",
      "247.42024801432896\n",
      "318.5568953760844\n",
      "387.72481330932317\n",
      "447.065118314664\n",
      "492.691733638023\n",
      "521.4493042315614\n",
      "551.6673529580174\n",
      "610.5917476526562\n",
      "702.9338802235022\n",
      "849.5099687345016\n",
      "1026.913660140866\n",
      "1218.9156769426315\n",
      "1390.5313187554077\n",
      "1484.6902518445129\n",
      "1515.114354003132\n",
      "1525.2368633065926\n",
      "1566.3807581948108\n",
      "1671.158845691246\n",
      "1771.407212308764\n",
      "1896.832331583799\n",
      "1976.9549716256624\n",
      "1923.794351381088\n",
      "1761.6178425495402\n",
      "1562.4549595293058\n",
      "1422.2404550569088\n",
      "1308.5970314857484\n",
      "1228.8102007817026\n",
      "1218.4826371836796\n",
      "1239.0136444415482\n",
      "1303.9512116245157\n",
      "1343.561817088328\n",
      "1400.3698683741904\n",
      "1341.4761205693694\n",
      "1156.1488515310689\n",
      "908.4965122275943\n",
      "689.7530001939417\n",
      "532.4977812543542\n",
      "415.12745412114583\n",
      "354.1757786470147\n",
      "312.58036910957316\n",
      "270.3604650872811\n",
      "228.88875888932984\n",
      "189.95221512470596\n",
      "153.1435863064925\n",
      "110.76328338269876\n",
      "72.98586844076353\n",
      "46.33821904981654\n",
      "30.367582785504876\n",
      "22.468980290303964\n",
      "16.79327007311773\n",
      "14.583009422531942\n",
      "11.341014255766018\n",
      "8.561783372640182\n"
     ]
    }
   ],
   "source": [
    "positive =  np.argwhere(processeddata['train'].gt == 0)\n",
    "positive\n",
    "for i in processeddata['train'].patch[positive,:,4,4].squeeze().sum(axis=0):print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7dde4d79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "processeddata['train'].gt[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fd564a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
